package jia.core.graphics;

import jia.game.GenericFlags;
import org.apache.log4j.Logger;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.util.Hashtable;

public abstract class AbstractWallImageParser implements WallImageParser {
    
    private static final Logger log = Logger.getLogger(AbstractWallImageParser.class);
    protected int[] PIXEL_SPEC = new int[4];
    protected Hashtable imgProps;
    
    // ===============================================
    /**
     * Takes an input Image, converts it to a BufferedImage in a transparent color
     * space if necessary, and sets all instances of the given color (rgb value)
     * to RGBA {0,0,0,0}.
     * <p/>
     * If at all possible, this method will return a modified version of the
     * original image.
     *
     * @param initImg    The image to process
     * @param transColor The color to make transparent
     * @return The BufferedImage with transparent pixels
     */
    protected BufferedImage setTransparency(BufferedImage initImg, int[] transColor) {
        int width = initImg.getWidth();
        int height = initImg.getHeight();
        
        if (!initImg.getColorModel().hasAlpha()) {
            BufferedImage newImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics g = newImg.getGraphics();
            g.drawImage(initImg, 0, 0, width, height, null);
            initImg = newImg;
        }
        
        WritableRaster myRaster = initImg.getRaster();
        int[] transPixel = new int[]{0, 0, 0, 0};
        
        for (int px = 0; px < width; px++) {
            for (int py = 0; py < height; py++) {
                int[] pixel = myRaster.getPixel(px, py, PIXEL_SPEC);
                if ((pixel[0] == transColor[0]) && (pixel[1] == transColor[1])
                && (pixel[2] == transColor[2])) {
                    myRaster.setPixel(px, py, transPixel);
                }
            }
        }
        
        return initImg;
    }
    
    // ====================================================
    /**
     * @param img
     * @param x
     * @param y
     * @param w
     * @param h
     * @return
     */
    protected BufferedImage makeSubimage(BufferedImage img,
        int x,
        int y,
        int w,
        int h) {
        if (GenericFlags.debugMessages) {
            log.debug("Pulling subimage from " + x + ", " + y + " with size " + w + "x" + h);
        }
        
        BufferedImage subImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        subImg.getGraphics().drawImage(img.getSubimage(x, y, w, h), 0, 0, null);
        return subImg;
    }
    
    //====================================================
    /**
     * @param img
     * @param x
     * @param y
     * @param w
     * @param h
     * @param mult
     * @return
     */
    protected BufferedImage makeSubimage(BufferedImage img,
        int x,
        int y,
        int w,
        int h,
        int mult) {
        if (GenericFlags.debugMessages) {
            log.debug("Pulling multiple subimage from " + x + ", " + y + " for " + mult + " width");
        }
        BufferedImage subImg = new BufferedImage(w * mult, h, BufferedImage.TYPE_INT_ARGB);
        for (int m = 0; m < mult; m++) {
            subImg.getGraphics().drawImage(img.getSubimage(x, y, w, h), m * w, 0, null);
        }
        return subImg;
    }
}
